package com.example.administrator.fishbone.Fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fishbone.R;
import com.example.administrator.fishbone.adapter.LinkmanAdapter;
import com.example.administrator.fishbone.bean.BackHandledFragment;
import com.example.administrator.fishbone.db.DishTable;
import com.example.administrator.fishbone.utils.LinkmanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class PersonnelFragment extends BackHandledFragment {
    ListView listView;
TextView textViewshow;
    ImageView imageView;
    TextView textconfirm;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_personnel, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setData();
        setListener();


    }
    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(getContext(), "第" + position + "位", Toast.LENGTH_SHORT).show();
                        textViewshow.setBackgroundResource(R.drawable.login_verfifycode_bg);
                        LinkmanUtils utils = (LinkmanUtils) parent.getItemAtPosition(position);
                        imageView = (ImageView) view.findViewById(R.id.person_item_image);
                        imageView.setTag(position);
                        if (utils != null) {
                            textViewshow.setText(utils.getTVname());
                            if (imageView != null) {
                                imageView.setImageResource(R.drawable.add_user_check);
                            }
                        }
            }
        });
        textconfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
//                Fragment_CreatNewTask fragment_creatNewTask = new Fragment_CreatNewTask();
                Fragment fragment = fm.findFragmentByTag("HomeAddFragment");
//                Bundle bundle = new Bundle();
                String name = textViewshow.getText().toString();
//                bundle.putString("key", name);
//                fragment_creatNewTask.setArguments(bundle);
                String tag = PersonnelFragment.this.getTag();
                EditText editText;
                switch (tag) {
                    case "pp":
                        editText = (EditText) fragment.getActivity().findViewById(R.id.add_task_edit_principal);
                        editText.setText(name);
                        break;
                    case "ppo":
                        editText = (EditText) fragment.getActivity().findViewById(R.id.add_task_edit_reviewer);
                        editText.setText(name);
                        break;
                    case "ddd":
                        editText = (EditText) fragment.getActivity().findViewById(R.id.add_task_edit_cc);
                        editText.setText(name);
                        break;
                }
                ft.remove(PersonnelFragment.this);
                ft.commit();

            }
        });
    }

            private void initView() {
                listView = (ListView) getView().findViewById(R.id.parentPanel_listView);
                textViewshow = (TextView) getView().findViewById(R.id.personnel_text);
                textconfirm = (TextView) getView().findViewById(R.id.personnel_confirm);

            }

            private void setData() {
                List<LinkmanUtils> util = new ArrayList<>();
                for (int i = 0; i < 60; i++) {

                    LinkmanUtils sinkmanUtils = new LinkmanUtils("name" + i, "last" + i, "_id" + i);
                    util.add(sinkmanUtils);
                }
                DishTable.dishTable().init(getContext());
                List<LinkmanUtils> mlist = DishTable.dishTable().getmList();
                LinkmanAdapter adapter = new LinkmanAdapter(getContext(), mlist);
                System.out.println("=====================================");
                listView.setAdapter(adapter);
            }

    @Override
    public boolean onBackPressed() {
        return false;

    }


//    private void myQuery(String selection, String[] selectionArgs) {
//        sqLiteDatabas = mydbHelper.getReadableDatabase();
//        cursor = sqLiteDatabas.query(MydbHelper.PERSON_TABLE,null,selection,selectionArgs
//        ,null,null,null);
//        SimpleCursorAdapter a = new SimpleCursorAdapter(getActivity(), R.layout.personnel_list_item,
//                cursor, new String[]{MydbHelper.PERSON_NAME}, new int[]{R.id.parentPanel_item_text_name});
//        listView.setAdapter(a);
//    }


}
